import type { List } from "lodash";
import type { chatStore } from "../store/chatStore";
import type { friendStore } from "../store/friendStore";
import type { userStore } from "../store/userStore";

// 工厂函数返回值类型
export interface FactoryResult {
    user_store: ReturnType<typeof userStore> | null,
    friend_store: ReturnType<typeof friendStore> | null,
    chat_store: ReturnType<typeof chatStore> | null
}

// 用户信息
export interface UserInfo {
    username: string,
    gex: string,
    uu_id: string,
    token: string,
    avatar: string,
    TypeOfFriendShips: string[],
    signatureOfTheIndividual: string,
    birthday: string | null,
    province: string | null,
    country: string | null,
    city: string | null
}



// 存储用户信息
export type UserBasicInfo = {
    avatarUrl: string,
    nickName: string,
    signature: string,
    sex: string,
    birthday: string,
    country: string,
    province: string,
    urban: string
}


// 定义用户信息接口
export interface CurrentUser {
    username: string;
    uu_id: string;
    gex: string;
    area: string;
    typeOfFriendShips: string[];
    avatar: string;
    signatureOfTheIndividual: string;
}

// 定义群聊信息接口
export interface CurrentGroup {
    create_at: Date | number | string
    group_id: string
    group_leader: string
    group_name: string
    group_type: string
    member_numbers: number
}


export interface ChatMessage {
    message: string,
    receiverId: string,
    senderId: string,
    timeStamp: string,
}
export type ChatData = {
    [key: string]: ChatMessage[]
}
export interface ChatDataWithCurrentuser {
    chatData: ChatData,
    currentUserId: string
}


// 好友列表菜单结构
interface ChildFriendMenu {
    avatar: string,
    signature: string,
    label: string,
    value: string
}
export type FriendMenuList = {
    label: string,
    value: string,
    children?: ChildFriendMenu[],
    expanded: boolean
}


// 推荐好友信息类型     也可以用来存储当前用户信息
export type RecommendAndUser = {
    username: string;
    uu_id: string;
    gex: string;
    area: string;
    typeOfFriendShips: string[];
    avatar: string;
    signatureOfTheIndividual: string;
};

// 用户好友详细信息接口
interface FriendDetails {
    username: string;       // 好友用户名
    gex: string | null;     // 好友性别
    area: string | null;        // 好友地区
    uu_id: string;      // 好友Id
    typeOfFriendShips: string[] | null;     // 好友交友类型
    avatar: string;     // 头像url
    signatureOfTheIndividual: string;   // 好友标签
}
// 用户单个好友信息接口
export interface UserFriendInfo {
    friendDetails: FriendDetails,
    remark: string,
    group: string,
    checked?: boolean,          // 保证这个对象可以扩展
}


// 这个是处理好友请求的类型，在用！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
export type FriendRequest = {
    timeStamp: string,
    fromUserId: string,
    requestId: string,
    message: string,
    username: string,
    friendAvatar: string
}
export type RecommendFriends = {
    username: string,
    uu_id: string,
    gex: string,
    area: string,
    avatar: string,
    typeOfFriendShips: string[],
    signatureOfTheIndividual: string
}

// export type ChatList = {
//     group_id?: string,
//     group_key?: string,
//     group_name?: string,
//     group_type?: string,
//     group_leader?: string,
//     groupAvatar?: string,
//     fromFriendAvatar?: string,
//     friendId?: string,
//     friendName?: string,
//     friendAvatar?: string,
//     toFriendName?: string,
//     type: string
// }
// 结构之后的类型
// 定义群聊类型
export interface GroupChat {
    group_id: string;
    group_key: string;
    group_name: string;
    group_type: string;
    group_leader: string;
    groupAvatar?: string;
    type: "group-chat";
    remark?: string;        // 这个可有可无,
    messages?: string           // 存储创建群聊的第一条信息没有则是已经存在群聊中
}

// 定义私聊类型
export interface PrivateChat {
    friendId: string;
    friendName: string;
    friendAvatar: string;
    type: "private-chat";
    remark?: string;        // 这个可有可无
}

// 定义临时聊天类型
export interface TempChat {
    tempId: string;
    tempName: string;
    tempAvatar?: string;
    type: "temp-chat";
}

// 定义 ChatList 类型为联合类型
export type ChatList = GroupChat | PrivateChat | TempChat;

export type FactoryReturn = {
    user_store: ReturnType<typeof userStore> | null,
    friend_store: ReturnType<typeof friendStore> | null,
    chat_store: ReturnType<typeof chatStore> | null
}

// transfer穿梭眶类型
export interface Option {
    key: number | string,
    label: string,
    disabled?: boolean,
    avatar?: string
}

// 消息类型
export interface FileType {
    fileName: string,
    fileSize: ArrayBuffer,
    fileType: string
}
export interface Chat {
    content: string,
    toChatId: string,
    messageType: string,
    userNickName?: string | null,
    useNickAvatar?: string | null,
    atList?: string[] | null
}

// 文件消息类型
export type ChatFileInfo = {
    senderId: string,
    fileName: string,
    fileUrl: string,
    receivedId: string,
    fileExtension: string,
    mimeType: string,
    fileKey: string,
    timeStamp: string
}


export interface FileChat {
    toChatId: string,
    messageType: string,
    fileInfo: FileType
}
export interface FileChunk {
    messageType: string,
    fileName: string,
    fileSuffix: string
    chunkNumber: number,
    totalChunks: number,
}

interface Menu {
    label: string,
    icon: string,
    url: string,
    key?: string
}
export interface MenuObject {
    [key: string]: Menu[]
}

// 交友标签类型
export type DatingTabs = {
    [key: string]: {
        label: string,
        checked: boolean,
        icon?: string       // 后期可以给每个标签添加图标
    }[]
}

// 群聊类型
export interface SubGroupType {
    label: string,
    url: string,
    icon?: string
}
export type GroupType = {
    label: string,
    url: string,
    icon?: string,
    subTypes: SubGroupType[]
}

// 存储群聊基本信息
export type PullGroupBasicInfo = {
    group_id: string,
    group_name: string,
    group_type: string,
    group_leader: string,
    member_numbers?: number,
    group_avatar?: string,
    group_members?: MembersInfo[],
    create_at?: Date | string,
    messages?: Array<{
        message_id: string,
        content: string,
        sender_id: string,
        time_stamp: string
    }>
}
// 群成员基本信息（可用于at列表）
export type MembersInfo = {
    key: string,
    label: string,
    avatar: string
}
export type GroupMembersList = Record<string, MembersInfo[]>

/*  --- 类型定义 --- */
export enum TargetChatType {
    Private = 'unidirectional',
    Group = 'Multi-directional'
}
export enum MsgType {
    TEXT = 'TEXT',
    IMAGE = 'IMAGE',
    FILE = 'FILE',
    GroupInvation = "GROUP-INVATION"
}

// websocket消息类型
// messageTypes.ts
export const MSG_TYPE = {
    UNIDIRECTIONAL: 'unidirectional',
    GROUP_JOINED: 'GROUP_JOINED',
    PRIVATE_FILE_METADATA: 'private-file-metadata',
    GROUP_FILE_METADATA: 'group-file-metadata',
    GROUP_FILE_ACK: 'group-file-ack',
    GROUP_CHAT_MESSAGE: 'GROUP_CHAT-MESSAGE',
    PRIVATE_FILE_ACK: 'private-file-ack',
    PRIVATE_MESSAGE_ACK: 'private-message-ack',
} as const;

// 消息体泛型
export interface BaseMessage<T = any> {
    type: keyof typeof MSG_TYPE;
    payload: T;
}
/* ---------- 群聊消息 ---------- */
export interface GroupMessage {
    id: string;
    groupId: string;
    from: string;
    message: string;
    userNickName?: string,
    userNickAvatar?: string,
    timeStamp: string;
    status?: "sent" | "received" | "pending" | "failed"; // 本地状态
    avatar?: string,
    type?: MsgType,
    invationList?: string[]
}

/* ---------- 单聊消息 ---------- */
export interface PrivateMessage {
    id: string;          // 后端 messageId
    chatId: string;      // 好友 id（与本人 id 拼接成的会话 id）
    from: string;        // 发送者 id
    to: string;          // 接收者 id
    message: string;
    timeStamp: string,
    userNickName?: string,
    userNickAvatar?: string,
    status: "sent" | "received" | "pending" | "failed", // 本地状态
    groupId?: null | string,
    atList?: null | List<string>,
    avatar?: string,
    type?: MsgType
}


/* ----------------stock Private Message----------------------- */
export interface StockPrivateMessage {
    chatId: string,
    senderId: string,
    receiverId: string,
    message: string,
    messageKey: string,
    userNickName?: string,
    userNickAvatar?: string,
    timeStamp: string,
    type: string
}
export type StockFriendObject = Record<string, StockPrivateMessage[]>

/* ----------------stock Public Message-------------------------------------------- */
export interface StockGroupMessage {
    groupId: string,
    messageKey: string,
    senderId: string,
    message: string,
    type: string,
    userNickName?: string
    userNickAvatar?: string,
    timeStamp: string,
    at: List<string>
}
export type StockGroupObject = Record<string, StockGroupMessage[]>


export enum FileSuffixType {
    Unknown = "unknown",
    Image = "image",
    Document = "document",
    Video = "video",
    Other = "other"
}





export type Music = {
    music_key: string,
    music_name: string,
    singer: string,
    music_type?: string,
    showExtra?: boolean
}


